/**
 * 
 */
package com.wsd.action.admin;

import java.util.Date;
import java.util.List;

import org.apache.struts2.json.annotations.JSON;

import com.opensymphony.xwork2.ActionContext;
import com.wsd.action.BaseAction;
import com.wsd.bean.OrderList;
import com.wsd.bean.ProductOrder;
import com.wsd.bean.Users;
import com.wsd.service.OrderService;

/**
 * @author Administrator
 *
 */
public class OrderAction extends BaseAction {
	
	private OrderService orderService;
	private boolean all;
	private ProductOrder order;
	private int start;
	private int limit;
	private int total;
	private boolean state;
	private int[] OIds;
	private int oid;
	private String key;
	private Date startDate;
	private Date endDate;
	@JSON(serialize = false)
	public OrderService getOrderService() {
		return orderService;
	}
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}
	@JSON(serialize = false)
	public boolean isAll() {
		return all;
	}
	public void setAll(boolean all) {
		this.all = all;
	}
	@JSON(serialize = false)
	public ProductOrder getOrder() {
		return order;
	}
	public void setOrder(ProductOrder order) {
		this.order = order;
	}
	@JSON(serialize = false)
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	@JSON(serialize = false)
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	@JSON(serialize = false)
	public boolean isState() {
		return state;
	}
	public void setState(boolean state) {
		this.state = state;
	}
	@JSON(serialize = false)
	public int[] getOIds() {
		return OIds;
	}
	public void setOIds(int[] ids) {
		OIds = ids;
	}
	@JSON(serialize = false)
	public int getOid() {
		return oid;
	}
	public void setOid(int oid) {
		this.oid = oid;
	}
	@JSON(serialize = false)
	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
	@JSON(serialize = false)
	public Date getStartDate() {
		return startDate;
	}
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}
	@JSON(serialize = false)
	public Date getEndDate() {
		return endDate;
	}
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
	@JSON(serialize = false)
	public String getOrders(){
		List<ProductOrder> orders = null;
		if(all){
			orders = orderService.findAll(start,limit);
			total = orderService.findAllCount();
		}else{
			orders = orderService.findByExample(order,start,limit);
			total = orderService.findByExampleCount(order);
		}
		if(orders != null){
			processOrders(orders);
		}
		result.put("orders", orders);
		return AJAX;
	}
	
	public String updateState(){
		orderService.updateState(OIds,state);
		return AJAX;
	}
	public String getOrderList(){
		List<OrderList> orderList = orderService.findOrderList(oid);
		result.put("orderList", orderList);
		return AJAX;
	}
	/**
	 * 订单详情
	 * @return
	 */
	public String detail(){
		List<OrderList> orderList = orderService.findOrderList(oid);
		ProductOrder order = orderService.findById(oid);
		Users user = order.getUsers();
		ActionContext.getContext().put("oList", orderList);
		for(OrderList item : orderList){
			System.out.println(item.getProducts().getPName());
		}
		ActionContext.getContext().put("order", order);
		ActionContext.getContext().put("user", user);
		return "detail";
	}
	
	public String search(){
		List<ProductOrder> orders = null;
		orders = orderService.search(key,startDate,endDate,start,limit);
		if(orders != null){
			processOrders(orders);
		}
		total = orderService.searchCoun(key,startDate,endDate);
		result.put("orders",orders);
		return AJAX;
	}
	
	
	public String history(){
		List<ProductOrder> orders = null;
		orders = orderService.history(startDate,endDate,start,limit);
		if(orders != null){
			processOrders(orders);
		}
		total = orderService.historyCount(startDate,endDate);
		result.put("orders",orders);
		result.put("orders",orders);
		return AJAX;
	}
	/**
	 * @param orders
	 */
	private void processOrders(List<ProductOrder> orders) {
		for(ProductOrder order : orders){
			Users user = new Users();
			user.setUName(order.getUsers().getUsername());
			user.setUserLevel(order.getUsers().getUserLevel());
			order.setUsers(user);
			order.setAdmin(null);
			order.setOrderLists(null);
		}
	}
	
	

}
